package com.zh.chat.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import net.sf.json.JSONArray;

import com.zh.chat.entity.UDPProtocol;


/**
 * 用来写XML和聊天记录文件的工具
 * @author Administrator
 *
 */
public class FileOperationUtil {
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	private static Map<Long,FileWriter> fws = new HashMap<Long,FileWriter>();
	private static FileWriter fw = null;
	
	/**
	 * 根据文件类型和用户ID生成不同的路径
	 * @param fileType
	 * @param id
	 * @return
	 */
	public static String getFilePath(int fileType, java.lang.Long id, String fileName){
		String path = null;
		switch (fileType) {
		case FileType.IMAGE:
			path = "data"+ File.separatorChar +"images"+File.separatorChar;
			break;
		case FileType.DATA:
			path = "data"+File.separatorChar + id +File.separatorChar;
			break;
		case FileType.ATTACHMENT:
			path = "attachment"+File.separatorChar;
			break;
		case FileType.SCULPTURE:
			path = "data"+ File.separatorChar + id +File.separatorChar+"sculpture"+File.separatorChar;
			break;
		case FileType.CHATLOG:
			path = "data"+ File.separatorChar + id +File.separatorChar+"chatlog"+File.separatorChar;
			break;
		default:
			path = "other"+File.separatorChar;
			break;
		}
		if(fileName!=null){
			path += fileName;
		}
		return path;
	}
	
	public static void appendChatLog(UDPProtocol request) throws IOException{
		 fw = fws.get(request.getRequestId());
		if(fw==null){//如果HashMap中存在filewrite对象就
			fw = new FileWriter(new File(request.getRequestId()+".txt"));
			fws.put(request.getRequestId(), fw);
		}
		String requester = "发送人："+request.getRequestId();
		String responser = "接收人："+request.getTargetId();
		String sendDate = "发送日期："+sdf.format(new java.util.Date());
		String msg = "内容："+request.getData().toString();
		fw.append(requester+" "+responser+" "+sendDate+"\n"+msg+"\n");
		fw.flush();
	}
	
	public static String readLogFile(String path){
		
		return path;
	}
	
	public static JSONArray getFileJSONArray(String path) throws FileNotFoundException{
		JSONArray jarr = null;
		InputStreamReader dis = null;
		try {
			InputStream fis = new FileInputStream(path);
			dis = new InputStreamReader(fis);
			if(dis!=null){
				BufferedReader br = new BufferedReader(dis);
				String json = "";
				String line = null;
				while ((line = br.readLine()) != null) {
					json+=line;
				}
				//关闭输入流
				dis.close();
				dis=null;
				br.close();
				br=null;
				jarr = JSONArray.fromObject(json);
			}
		} catch (FileNotFoundException e) {
			throw e;
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				if(dis!=null){
					dis.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return jarr;
	}
	
	public static void close() throws IOException{
		Iterator<Long> it = fws.keySet().iterator();
		while (it.hasNext()) {
			fw.flush();
			fws.get(it.next()).close();
		}
	}
}
